<template>
    <div style="display: flex;">
        <div class="motor" ref="motorRef"></div>
        <div style="width: 200px;">转速：{{ motorInfo?.value }} | 方向：{{ motorInfo?.value > 0 ? '正转' : '反转' }}
        </div>
    </div>
</template>

<script setup>

import { ref, watch } from 'vue'
const motorRef = ref(null)
const props = defineProps({
    motorInfo: {
        type: Object,
        default: {}
    },
})

watch(() => props.motorInfo, (newVal) => {
    const { value } = newVal;
    const motorEl = motorRef.value;
    if (motorEl) {
        motorEl.classList.remove('left', 'right');
    }

    const direction = value > 0 ? "left" : "right"
    const duration = 1000 / value;

    motorEl.style.animationDuration = `${duration}s`;
    motorEl.classList.add(direction);


}, { deep: true })
</script>

<style lang='less' scoped>
.motor {
    width: 50px;
    height: 50px;
    background-color: #000;
    color: #fff;
    border-radius: 50%;
    position: relative;

    &::before {
        content: "";
        width: 20px;
        height: 10px;
        background-color: #000;
        position: absolute;
        top: 40%;
        right: -20%;
    }

    &.left {
        animation: left linear infinite;
    }

    &.right {
        animation: right linear infinite;
    }
}

@keyframes left {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

@keyframes right {
    0% {
        transform: rotate(360deg);
    }

    100% {
        transform: rotate(0deg);
    }
}
</style>